<?
// Loading Modules and Admin config
require_once(dirname(__FILE__) ."/common/inc.init.php");

// Loading Recaptchalib
require(DIR_INCLUDES.'/lib/recaptchalib.php');
$publickey  = '6LeCHb0SAAAAAPWl7az87f5NOe8_41CPCIMalNPu';
$privatekey = '6LeCHb0SAAAAAEA_WweP3M6MVGYlVmOX0BGZ88UI';
$attempts   = 3; // cantidad de intentos;

$lang   = new adminLang();
$failed = new SessionManager();

if($_POST["action"]=="login")
{
    $lang->setLang($_POST['lang']);
    if($_POST["username"] && $_POST["password"])
    {
       // Creating database conetion
        $dbLink = connectDb();
        if(!$dbLink) die('Problema con la base de datos al querer hacer el login.');

        // Starting user validation
        $user = new adminUser();
        $user->setDbLink($dbLink);

        // Muestro captcha:
        $captcha = true;
        if($failed->exists('failed_login_count') && $failed->get('failed_login_count') >= $attempts)
        {
            $resp    = recaptcha_check_answer($privatekey, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);   
            $captcha = $resp->is_valid;
        }

        if($user->login($_POST['username'], $_POST['password']) && $user->active=='Y' && $captcha)
        {
            new adminLog($dbLink, 'admin', 'system login', $user->getID());
            
            $failed->set('failed_login_count', 0);
            header("Location: ". $GLOBALS["CONF"]["url"]["admin"].'#'.$_POST['moduleID']);
            exit();
        }
        else
        {
            $errorMsg = '"'. $lang->translate("login_failed_for_user").'."';
            
            $failed_login_count = ($user->failed_login_count > $failed->get('failed_login_count')) ? $user->failed_login_count : $failed->get('failed_login_count');
            $failed->set('failed_login_count', $failed_login_count + 1);
        }
    }
    $errorMsg = '"'. $lang->translate("invalid_login_data").'."';
}

if($_GET["action"]=="logout")
{
    $user = new adminUser();
    $user->logout();
}

// lanzo este header para que me redireccione en caso de que sea un request de Ajax del EXTjs.
header("HTTP/1.0 401 Authorization Required");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?= $GLOBALS["website"]["name"] ?></title>
    <meta name="description" content="mobile first, app, web app, responsive, admin dashboard, flat, flat ui">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" href="css/font.css" cache="false">
    <link rel="stylesheet" href="css/style.css">
    <link rel="shortcut icon" href="<?$GLOBALS['CONF']['url']['root']?>/img/favicon.png" type="image/png" />
    <!--[if lt IE 9]>
    <script src="js/ie/respond.min.js" cache="false"></script>
    <script src="js/ie/html5.js" cache="false"></script>
    <![endif]-->
    <style>
        @font-face {
          font-family: Engravers;
          src: url("../Font/IE/Engravers_Gothic.eot");
          /* IE9 Compat Modes */
          src: url("../Font/IE/Engravers_Gothic.eot?#iefix") format("embedded-opentype"), url("../Font/Engravers_Gothic.woff") format("woff"), url("../Font/Engravers_Gothic.ttf") format("truetype"), url("../Font/Engravers_Gothic.svg#Engravers_Gothic") format("svg");
          /* Legacy iOS */
        }
        .navbar-brand{
            font-family: Engravers,'Engravers Gothic',Helvetica,Sans-Serif !important;
            padding: 0;
            color: #222222;
            display: block;
            font-weight: normal;
            font-style:normal;
            margin:15px 0 0 10px;
        }
        .navbar-brand:hover,.navbar-brand:focus,.navbar-brand:active{color: #222222;}
    </style>
</head>
<body>
    <!-- header -->
    <header id="header" class="navbar bg">
        <a class="navbar-brand" href="<?=$GLOBALS['CONF']['url']['root']?>/admin" style="padding:0"><b>REVOLUCI&Oacute;N PELOTA</b> ADMIN</a>
    </header>
    <!-- / header -->
    <section id="content">
        <div class="main padder">
            <div class="row">
                <div class="col-lg-4 col-lg-offset-4 m-t-large">
                    <section class="panel">
                        <header class="panel-heading text-center">Ingreso</header>
                        <? if ($errorMsg){ ?>
                            <div class="row-fluid">
                                <div class="span8 offset2">
                                    <div class="alert alert-error alert-block">
                                        <p class="text-center">
                                            <i class="icon-warning-sign"></i> <strong><?= ucfirst($lang->translate("error_login_in")) ?>: </strong><?= $errorMsg ?>
                                        </p>
                                    </div>
                                </div>
                            </div>
                        <? } ?>
                        <form method="post" action="login.php" class="panel-body">
                            <div class="block">
                                <label class="control-label"><?= ucfirst($lang->translate("user")) ?></label>
                                <input type="text" id="username" name="username" placeholder="<?= ucfirst($lang->translate("user")) ?>" class="form-control">
                            </div>
                            <div class="block">
                                <label class="control-label"><?= ucfirst($lang->translate("password")) ?></label>
                                <input type="password" name="password" id="inputPassword" placeholder="<?= ucfirst($lang->translate("password")) ?>" class="form-control">
                            </div>
                            <?
                            if($failed->exists('failed_login_count') && $failed->get('failed_login_count') >= $attempts){
                            ?>
                                <div class="block">
                                    <? echo recaptcha_get_html($publickey);?>
                                </div>
                            <? } ?>
                            <button type="submit" class="btn btn-info"><?= ucfirst($lang->translate("Log In")) ?></button>
                            <input type="hidden" name="moduleID" id="moduleID" value="<? echo $_POST['moduleID']; ?>">
                            <input type="hidden" name="action" value="login">
                        </form>
                    </section>
                </div>
            </div>
        </div>
    </section>
    <!-- footer -->
    <!--<footer id="footer">
        <div class="text-center padder clearfix">
            <p>
                <a href="https://twitter.com/GreenTomatoST" class="btn btn-xs btn-circle btn-twitter"><i class="icon-twitter"></i></a>
                <a href="https://es-es.facebook.com/GreenTomatoST" class="btn btn-xs btn-circle btn-facebook"><i class="icon-facebook"></i></a>
            </p>
        </div>
    </footer>-->
    <!-- / footer -->
    <script src="js/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="js/bootstrap.js"></script>
    <!-- app -->
    <script src="js/app.js"></script>
    <script src="js/app.plugin.js"></script>
    <script src="js/app.data.js"></script>
</body>
</html>
